package com.yuerlive.rainbow.module.live.adapter;

import android.content.Context;
import android.media.tv.TvContentRating;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.common.BaseRecycleViewAdapter;
import com.yuerlive.rainbow.entity.ContributionData;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.widget.CircleImageView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017/6/14.
 */

public class ContributionsAdapter extends BaseRecycleViewAdapter {

    private List<ContributionData> mDatas;
    private static final int TYPE_FIRST = 0;
    private static final int TYPE_NORMAL = 1;
    private static final int TYPE_EMPTY = -1;


    public ContributionsAdapter(Context mContext, List<ContributionData> data) {
        super(mContext);
        this.mDatas = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_EMPTY) {
            return new EmptyHolder(mInflater.inflate(R.layout.layout_exception, parent, false));
        } else if (viewType == TYPE_FIRST) {
            return new FirstViewHolder(mInflater.inflate(R.layout.item_contributions_first, parent, false));
        } else {
            return new NormalViewHolder(mInflater.inflate(R.layout.item_contributions_normal, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_FIRST:
                initFirstView((FirstViewHolder) holder, position);
                break;
            case TYPE_NORMAL:
                initNormalView((NormalViewHolder) holder, position);
                break;
        }
    }

    private void initFirstView(FirstViewHolder holder, int position) {
        ContributionData info = mDatas.get(position);
        GlideUtil.load(mContext, info.getIcon(), holder.imgHeader);
        holder.tvNickName.setText(info.getNickname());
        if (info.getSex() == 0) {
            holder.tvNickName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_boy, 0);
        } else {
            holder.tvNickName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_girl, 0);
        }
        holder.tvValue.setText(Html.fromHtml(mContext.getResources().getString(R.string.format_first_contribution, "" + info.getCharmValue())));
    }

    private void initNormalView(NormalViewHolder holder, int position) {
        ContributionData info = mDatas.get(position);
        GlideUtil.load(mContext, info.getIcon(), holder.imgHeader);
        holder.tvNickName.setText(info.getNickname());
        if (info.getSex() == 0) {
            holder.tvNickName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_boy, 0);
        } else {
            holder.tvNickName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_girl, 0);
        }
        if (position >= 3) {
            holder.imgRank.setVisibility(View.GONE);
            holder.tvRank.setVisibility(View.VISIBLE);
        } else {
            holder.imgRank.setVisibility(View.VISIBLE);
            holder.tvRank.setVisibility(View.GONE);
        }
        holder.tvValue.setText(Html.fromHtml(mContext.getResources().getString(R.string.format_normal_contribution, "" + info.getCharmValue())));
        holder.tvRank.setText("" + (position + 1));
        if (position == 1) {
            holder.imgRank.setImageResource(R.drawable.rank_2);
        } else {
            holder.imgRank.setImageResource(R.drawable.rank_3);
        }
    }

    @Override
    public int getItemCount() {
        if (mDatas == null || mDatas.isEmpty()) {
            return 1;
        }
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas == null || mDatas.isEmpty()) {
            return TYPE_EMPTY;
        } else {
            if (position == 0) return TYPE_FIRST;
        }
        return TYPE_NORMAL;
    }

    static class FirstViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imgRank)
        ImageView imgRank;
        @BindView(R.id.tvRank)
        TextView tvRank;
        @BindView(R.id.imgHeader)
        CircleImageView imgHeader;
        @BindView(R.id.tvNickName)
        TextView tvNickName;
        @BindView(R.id.tvValue)
        TextView tvValue;

        public FirstViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class NormalViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.imgRank)
        ImageView imgRank;
        @BindView(R.id.tvRank)
        TextView tvRank;
        @BindView(R.id.bottomline)
        View bottomline;
        @BindView(R.id.imgHeader)
        CircleImageView imgHeader;
        @BindView(R.id.tvNickName)
        TextView tvNickName;
        @BindView(R.id.tvValue)
        TextView tvValue;

        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
